package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.common.redis.RedisUtils;
import com.fuhe.chen.vendingmachine.dao.CommodityDao;
import com.fuhe.chen.vendingmachine.dao.ContainerDao;
import com.fuhe.chen.vendingmachine.dto.ItemInMachine;
import com.fuhe.chen.vendingmachine.dto.cond.CommodityCond;
import com.fuhe.chen.vendingmachine.pojo.Commodity;
import com.fuhe.chen.vendingmachine.service.ICategoryService;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.fuhe.chen.vendingmachine.constant.CacheConstant.COMMODIIES_ALL_ON_SALE;

@Service
public class CommodityServiceImpl implements ICommodityService {

    @Autowired
    CommodityDao commodityDao;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ICategoryService categoryService;

    @Autowired
    ContainerDao containerDao;

    @Override
    public void addCommoditySoldToCache(String tradeNo, Map<ItemInMachine, Integer> commoditySolds, String machineId) {
        commoditySolds.forEach((item,count)->{
            String field = String.valueOf(item.getId());
            String value = item.getPrice()+"_"+count+"_"+item.getCategoryName()+"_"+item.getItem_name()+"_"+machineId+"_"+item.getContainerId();
            redisUtils.hset(tradeNo,field,value);
            //过期时间:一天
            redisUtils.expire(tradeNo,60*60*24);
        });
    }

    @Override
    public List<Commodity> queryAllOnSale() {
        String key = COMMODIIES_ALL_ON_SALE;
        if (redisUtils.hasKey(key)){
            return (List<Commodity>)redisUtils.get(key);
        }else{
            List<Commodity> commodities = commodityDao.findAllOnSale();
            redisUtils.set(key,commodities);
            return commodities;
        }
    }

    @Override
    public PageInfo<Commodity> queryCommodityByCondition(CommodityCond cond, int pageNum, int size) {
        PageHelper.startPage(pageNum,size);
        List<Commodity> commodities = commodityDao.findByCondition(cond);
        PageInfo<Commodity> commodityPageInfo=new PageInfo<>(commodities);
        return commodityPageInfo;
    }

    @Override
    public Commodity queryOne(Integer id) {
        return commodityDao.findOne(id);
    }

    @Override
    public String addCommodity(Commodity commodity) {
        commodityDao.addCommodity(commodity);
        redisUtils.delAll(COMMODIIES_ALL_ON_SALE);
        return String.valueOf(commodity.getId());
    }

    @Override
    public void updateCommodity(Commodity commodity) {
        commodityDao.updateCommodity(commodity);
        redisUtils.delAll(COMMODIIES_ALL_ON_SALE);
    }

    @Override
    @Transactional
    public void deleteCommodity(Integer commodityId) {
        commodityDao.deleteCommodity(commodityId);

        //在售商品信息改变,需要清除相关缓存
        redisUtils.delAll(COMMODIIES_ALL_ON_SALE);
    }

    @Override
    public PageInfo<Commodity> findCommodityPageByLabel(Integer labelId,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Commodity> commodityListByLabel = commodityDao.findCommodityListByLabel(labelId);
        PageInfo<Commodity> commodityPageInfo = new PageInfo<>(commodityListByLabel);
        return commodityPageInfo;
    }


}
